package study.nosql.redis.support.aop;

import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
//@Component
@Aspect
public class OpsAspect {

    @Around("target(org.springframework.data.redis.connection.RedisConnection)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Stopwatch stopwatch = Stopwatch.createStarted();
        Signature signature = joinPoint.getSignature();
        String shortName = signature.toShortString();
        log.info("{}", joinPoint.getArgs());
        try {
            Object result = joinPoint.proceed();
            return result;
        } catch (Throwable ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            log.info("{} cost time [{}] ms", shortName, stopwatch.elapsed(TimeUnit.MILLISECONDS));
        }
    }
}
